import get_menus from '@dfeidao/atom-nodejs/authorization/get-menus';
import get_params from '@dfeidao/atom-nodejs/authorization/get-params';
import { IFeidaoAiNodejsComponent } from '@dfeidao/atom-nodejs/interfaces';
import nodejs from '@dfeidao/atom-nodejs/msg/nodejs';
import whoami from '@dfeidao/atom-nodejs/msg/whoami';
import render from '@dfeidao/atom-nodejs/render/render';
import { file_url, spaceid as productid } from '../../atom/config';
import P01 from './p01';
// import P04 from './p04';
import P02 from './p02';
import P03 from './p03';

export interface T {
	_id: string;
	menu_no: string;
	menu_name: string;
	client_type: string;
	parent_no: string;
	icon: string;
	show_type: string;
	description: string;
	priority: number;
	is_single: boolean;
	is_show: boolean;
	is_offline: boolean;
	is_guest: boolean;
	productid: string;
	systemid: string;
	formid: string;
	background: string;
	refsystemid: string;
	formname: string;
	package_no: string;
	package_name: string;
	package_url: string;
	auth_code: number;
	// c: [];
	// c: T[];
}

function render_data(data: Array<{ parent_no: string; menu_no: string; menu_name: string; package_url: string; icon: string; }>, p_name: string, parent_no2: string, menu_name2: string) {
	let res = '';
	function get_first_a(menu_name: string, icon: string, package_url: string, menu_no: string) {

		let h = ' href="' + package_url + '" style="cursor:default;width: 100%;color:#fff;display: inline-flex;align-items: center;justify-content: center;"';
		if (!package_url || package_url === 'undefined' || package_url === 'null') {
			h = ' style="cursor:default;width: 100%;display: inline-flex;align-items: center;justify-content: center;"';
		}
		return `<span class="display-ib text-c cursor-p nav onemenu pos-r ht60" style="width:130px;display: inline-flex;align-items: center;"><a class=" ht60 display-ib ${menu_no === parent_no2 ? 'bg-themeff' : ''}" ${h}><i class="iconfont ${icon} font20 vertical-a-t display-ib"></i><span class="font16 vertical-a-t display-ib" style="margin-left:10px;">${menu_name}</span></a>`;
	}
	function get_li(menu_name: string, package_url: string) {
		let h = ' href="' + package_url + '"';
		if (!package_url || package_url === 'undefined' || package_url === 'null') {
			h = ' style="cursor:default"';
		}
		return `<a' ${h}  '><li class="pos-r ${menu_name === menu_name2 ? 'bg-themef' : ''}" >${menu_name}</li></a>`;
	}
	let p_m = '';
	const pas = {};
	const check = {};
	const len = {};
	select_data("");
	if (pas[p_m] === 0) {
		res = res + '</li>';
	} else {
		res = res + '</li></ul></span>';
	}

	function select_data(parent_no: string) {
		data.forEach((item) => {
			const menu_no = item.menu_no;
			const _parent_no = item.parent_no;
			const menu_name = item.menu_name;
			const package_url = item.package_url;
			const icon = item.icon;
			if (parent_no === _parent_no) {
				const p_f_d = filter_data(menu_no);
				if (parent_no === "") {
					if (pas[p_m]) {
						if (pas[p_m] === 0) {
							res = res + '</li>';
						} else {
							res = res + '</ul></span>';
						}
					} else {
						pas[menu_no] = p_f_d.length;
					}
					p_m = menu_no;
					res = res + get_first_a(menu_name, icon, package_url, menu_no);
				} else {
					if (!check[_parent_no]) {
						check[_parent_no] = 1;
					} else {
						check[_parent_no] = check[_parent_no] + 1;
					}
					res = res + get_li(menu_name, package_url);
				}
				if (p_f_d.length > 0) {
					res = res + '<ul class="pos-a font16" style="width:130px;z-index: 99;top:60px;">';
				} else {
					res = res + '</li>';
					len[_parent_no] = filter_data(_parent_no).length;
					if (check[_parent_no]) {
						if (len[_parent_no] === check[_parent_no]) {
							res = res + '</ul></span>';
						}
					} else {
						res = res + '</span>';
					}
				}
				select_data(menu_no);
			}
		});
	}

	function filter_data(p_no: string) {
		const filter = data.filter((item) => {
			const _parent_no = item.parent_no;
			return _parent_no === p_no;
		});
		return filter;
	}
	const obj = {} as { P04: string };
	obj[p_name] = res;
	return obj;
}


export default async function nba001(fd: IFeidaoAiNodejsComponent) {
	const users = await whoami(fd.data.actionid, fd.data.sessionid);
	const user = await get_params(fd.data.msg, fd.data.headers, users.productid, '3');
	const menus = await get_menus(fd.data.msg, fd.data.headers, users.productid, '3') as T[];

	const e_url = fd.data.url;
	const page_name = e_url.match(/.*\/((.*?)\.html)/)[1];
	const a = menus.filter((i) => {
		if (i.package_url === page_name) {
			return i;
		}
	});

	// console.log(a[0].menu_no);
	const parent_no2 = a[0].parent_no;
	const menu_name2 = a[0].menu_name;

	const menus_show = menus.filter((i) => {
		return i.is_show === true;
	});

	render(fd.data.node, menus, render_data(menus_show, 'P04', parent_no2, menu_name2).P04, 'P04');
	// console.log(arr);
	render(fd.data.node, JSON.stringify(user), P02, 'P02');

	const res2 = (await nodejs<{ headimg: string }>(fd.data.actionid, fd.data.sessionid, 'common/zjc-000003/s002', { staff_no: user.staff_no, productid: user.productid })) || {};
	render(fd.data.node, { ...user, ...res2 }, P01, 'P01');

	const logoarr = await nodejs<Array<{ logo: string }>>(fd.data.actionid, fd.data.sessionid, 'common/zjc-000001/s001', { productid: users.productid });
	if (logoarr.length > 0 && logoarr[0].logo) {
		render(fd.data.node, { logo: file_url + `getfile?productid=${productid}&id=${logoarr[0].logo}` }, P03, 'P03');
	} else {
		render(fd.data.node, { logo: './images/logo.png' }, P03, 'P03');
	}
}
